﻿//Ext.BLANK_IMAGE_URL = '../Scripts/ExtJs/resources/themes/images/gray/s.gif';
Ext.QuickTips.init();
Ext.Loader.setConfig({ enabled: true });

Ext.onReady(function () {

    var topBar = Ext.create("Ext.panel.Panel", {
        height: 100,
        region: "north",
        items: [{}],
        bbar: {
            height: 30,
            maxHeight: 30,
            enableOverflow: true,
            items: [{
                xtype: "button",
                text: "Kratos admin manager",
                cls: "x-button-center",
                icon: "Content/icons/house.png",
                handler: function () {
                    Ext.MessageBox.alert("title", "预留function");
                }
            }, "-", {
                xtype: "button",
                text: "Home",
                cls: "x-button-center",
                icon: "Content/icons/house.png"
            }, "-", {
                xtype: "button",
                text: "张奎",
                cls: "x-button-center",
                icon: "Content/icons/user_gray.png"
            }, "->", {
                xtype: "button",
                text: "刷新当前页",
                cls: "x-button-center",
                icon: "Content/icons/arrow_refresh.png"
            }, "-", {
                xtype: "button",
                text: "全部关闭",
                cls: "x-button-center",
                icon: "Content/icons/cross.png"
            }, "-", {
                xtype: "button",
                text: "附加到快速栏",
                cls: "x-button-center",
                icon: "Content/icons/basket_put.png"
            }, "-", {
                xtype: "button",
                text: "修改密码",
                cls: "x-button-center",
                icon: "Content/icons/key_go.png"
            }, "-", {
                xtype: "button",
                text: "注销",
                cls: "x-button-center",
                icon: "Content/icons/lock_open.png"
            }, "-", {
                xtype: "button",
                text: "退出",
                cls: "x-button-center",
                icon: "Content/icons/monitor_delete.png"
            }]
        }
    });

    var menu = Ext.create('Ext.panel.Panel', {
        title: '控制面板',
        width: 220,
        height: 300,
        minWidth: 220,
        maxWidth: 220,
        iconCls: 'dashboard',
        collapsible: true,
        split: true,
        layout: 'accordion',
        style: 'margin-top:1px',
        region: "west",
        defaults: {
            // applied to each contained panel
            bodyStyle: 'padding-left:0px; padding-top:10px'
        },
        layoutConfig: {
            // layout-specific configs go here
            titleCollapse: false,
            animate: true,
            activeOnTop: true
        }
    });

    //var waitMask = new Ext.LoadMask(Ext.getBody(), { msg: "系统正在处理数据，请稍候..." });  //定义一个
    //waitMask.show(); //show方法显示
    Ext.Ajax.request({
        url: 'Admin/Home/GetMenuByParentId', //获取一级菜单
        params: {
            parentId: -1
        },
        success: function (response, opts) {
            var roots = Ext.JSON.decode(response.responseText);
            Ext.Array.each(roots, function (item) {
                menu.add(Ext.create("Ext.tree.Panel", {
                    title: item.menuName,
                    iconCls: item.prevIcon,
                    autoScroll: true,
                    rootVisible: false,
                    lines: false,
                    store: createStore(item),
                    listeners: {
                        "itemclick": function (tree, record, it, index, e, eOpts) {
                            if (record.data.leaf) {
                                var tab = tabMain.getComponent('tab_' + record.data.id);
                                if (!tab) {
                                    tab = Ext.create("Ext.panel.Panel", {
                                        layout: "fit",
                                        id: 'tab_' + record.data.id,
                                        closable: true,
                                        autoScroll: true,
                                        border: false,
                                        loadMask: true,
                                        title: record.data.text,
                                        autoLoad: {
                                            url: record.data.url,
                                            scope: this,
                                            scripts: true,
                                            text: '页面加载中，请稍候....'
                                        }
                                    });
                                    tabMain.add(tab);
                                }
                                tabMain.setActiveTab(tab);
                            }
                            return false;
                        }
                    }
                }));
            });
            //waitMask.hide();
        },
        failure: function (response, opts) {
            alert(response.responseText);
            //waitMask.hide();
        }
    });

    //读取内存中的数据
    var createStore = function (item) {
        return Ext.create("Ext.data.TreeStore", {
            proxy: {
                type: "memory",
                reader: {
                    type: "json",
                    root: "children"
                }
            },
            root: item,
            fields: [//为了获取url，我不得不这么做
                    {name: 'text', type: 'string' },
                    { name: 'url', type: 'string' },
                    { name: 'leaf', type: 'boolean' }
            ],
            storeId: "menu_" + item.id
        });
    };

    var tabMain = Ext.create('Ext.tab.Panel', {
        id: "Admin_Layout_TabMain",
        style: 'margin:1px',
        autoScroll: true,
        enableTabScroll: true,
        bodyStyle: "padding:10px",
        plain: true,
        activeTab: 0,
        region: "center",
        items: [{//default xtype is usually panel.
            id: 'tab-0000',
            title: '首页',
            autoScroll: true,
            layout: 'fit',
            border: false,
            iconCls: 'house',
            autoLoad: {
                url: 'Test',
                scope: this,
                scripts: true,
                text: '页面加载中,请稍候....'
            }
        }]
    });

    Ext.create("Ext.container.Viewport", {
        id: "Admin_Layout_ViewPort",
        layout: 'border',
        items: [tabMain, menu, topBar]
    });

});